<?php
class Model_Table_User extends Zend_Db_Table{
	
	protected $_name = 'users';
	protected $_primary = 'id';
	
	public function allUser($options = null){
		$db = $this->getAdapter();
		$select = $db->select()->from('users AS u')->joinLeft('user_group AS g', 'u.group_id = g.id', 'g.name AS group_name');		
		$result = $db->fetchAll($select);
		return $result;
	}
	public function getUser($arrParam = null,$options = null){
		$db = $this->getAdapter();
		$select = $db->select()->from('users')->where('id =' . $arrParam['id']);
		return $db->fetchRow($select);
	}
	public function addUser($arrParam = null,$options = null){		
		$row = $this->fetchNew();
		$row->username = $arrParam['username'];
		$row->password = md5($arrParam['password']);
		$row->avatar = $arrParam['avatar'];
		$row->email = $arrParam['email'];
		$row->firstname = $arrParam['firstname'];
		$row->lastname = $arrParam['lastname'];
		$row->status = 1;
		$row->save();
	}
	public function deleteUser($arrParam = null,$options = null){
		if($options == 'multy'){
			$cid[] = $arrParam['cid'];
			$ids = $cid[0];
			foreach ($ids as $val){
				$this->deleteOneUser($val);
			}			
		}else{
			$this->deleteOneUser($arrParam['id']);
		}
	}
	public function deleteOneUser($id){
		$where = 'id =' . $id;
		$this->delete($where);
	}
	public function editUser($arrParam = null,$options = null){		
		$where = 'id =' . (int)$arrParam['id'];
		$row = $this->fetchRow($where);
		$row->username = $arrParam['username'];
		$row->password = md5($arrParam['password']);
		$row->avatar = $arrParam['avatar'];
		$row->email = $arrParam['email'];
		$row->firstname = $arrParam['firstname'];
		$row->lastname = $arrParam['lastname'];
		$row->status = $arrParam['status'];
		$row->save();
	}
	public function changeStatus($arrParam = null,$options = null){
		$status = $arrParam['status'];
		$where = 'id =' . (int)$arrParam['id'];
		$row = $this->fetchRow($where);
		$row->status = $status;
		$row->save();
	}
}